 /*
 * @description: 单列图片
 * @Author: along
 * @Date: 2019-09-08
 * @Last Modified by: along
 * @Last Modified time: 2020-09-11
 */
<template>
    <div
        class="single"
        @click="emitEvent(1)"
        v-if="isLoad"
        :style="{background: baseconfig.style.backgroundColor, paddingTop: `${baseconfig.style.marginV / 2}px`, paddingBottom: `${baseconfig.style.marginV / 2}px`}"
    >
        <!-- 有数据 -->
        <img
            :src="baseconfig.data.image_url"
            :style="{width: `${375 - baseconfig.style.marginH}px`, marginLeft: `${baseconfig.style.marginH / 2}px`, marginRight: `${baseconfig.style.marginH / 2}px`, borderRadius: rate}"
        >
        <!-- 无数据 -->
        <div v-if="!baseconfig.data.image_url">
            <img
                src="@/assets/image/single.png"
                class="single-none-image"
            >
        </div>
    </div>
</template>

<script>
export default {
    name: 'Single',
    props: {
        baseconfig: {
            type: Object,
            default: () => {}
        },
        index: {//存在多个相同组件,查询当前位置
            type: Number,
            default: () => {}
        }
    },
    data () {
        return {
            rate: '0px 0px 0px 0px',
            isLoad: false
        };
    },
    watch: {
        //圆角单位转换
        baseconfig:  {
            handler: function (val, oldVal) {
                let rateValue = val.style.borderRadius;

                this.rate = rateValue.replace(/rpx/g, 'px');
                this.isLoad = true;
            },
            deep: true
        }
    },
    mounted () {
        this.isLoad = true;
        if(JSON.stringify(this.baseconfig.style) !== '{}') {//回填圆角
            let rateValue = this.baseconfig.style.borderRadius;

            this.rate = rateValue.replace(/rpx/g, 'px');
        }
    },
    methods: {
        /**
         * @description 点击组件，向上级传递点击类型
         */
        emitEvent (type) {
            this.$emit('emit', {
                type: type,//组件类型
                index: this.index//组件位置
            });
        }
    }
};
</script>

<style lang="less" scoped>
.single {
    width: 375px;
    box-sizing: border-box;
    border: 1px #ffffff solid;
    display: flex;
    flex-direction: column;
    align-items: center;
    //无数据
    .single-none-image {
        width: 355px;
        height: 170px;
    }
}
</style>